import {
  updateSchoolInfo,
  getSchoolInfo
} from "@/api/Home/index";
import { setSchoolInfo } from "@/utils/author";

const school = {
  state: {
    schoolId: 'c1c94a00-1927-4429-8fee-0fff816b18fb',
    schoolName: '云南师范大学',
    areaCode: 'YNNU'
  },
  mutations: {
    SET_SCHOOL_ID: (state, schoolId) => {
      state.schoolId = schoolId;
    },
    SET_SCHOOL_NAME: (state, schoolName) => {
      state.schoolName = schoolName;
    },
    SET_AREA_CODE: (state, areaCode) => {
      state.areaCode = areaCode;
    }
  },
  actions: {
    UpdateSchoolInfo({ commit }, item) {
      const { openId, schoolId, schoolName } = item;
      return new Promise((resolve, reject) => {
        updateSchoolInfo(openId, schoolId)
          .then((result) => {
            commit('SET_SCHOOL_ID', schoolId);
            commit('SET_SCHOOL_NAME', schoolName);

            resolve();
          })
          .catch((err) => {
            console.log(err);
            reject();
          });
      })
    },
    getSchoolInfo({ commit }, school_id) {
      return new Promise((resolve, reject) => {
        getSchoolInfo(school_id).then((result) => {
          const { schoolId, schoolName, areaCode } = result.data;
          commit('SET_SCHOOL_ID', schoolId);
          commit('SET_SCHOOL_NAME', schoolName);
          commit('SET_AREA_CODE', areaCode);
          setSchoolInfo(result.data);
          resolve();
        }).catch((err) => {
          commit('SET_SCHOOL_ID', '');
          commit('SET_SCHOOL_NAME', '');
          commit('SET_AREA_CODE', '');
          setSchoolInfo('');
          reject();
        });
      })

    }
  }
}

export default school;